#include <pthread.h>
#include <semaphore.h>
#include <stdio.h>
#include <unistd.h>

int ticketAmout = 2; // 票的数量: 全局变量
sem_t mutex;

void *ticketAgent(void *arg) {
  sem_wait(&mutex);
  int t = ticketAmout;

  if (t > 0) {
    printf("One ticket sold\n");
    t--;
  } else {
    printf("Ticket sold out\n");
  }
  ticketAmout = t;
  sem_post(&mutex);
  pthread_exit(0);
}
int main(int argc, char const *agrv[]) {
  pthread_t ticketAgent_tid[2];
  sem_init(&mutex, 0, 1);
  for (int i = 0; i < 2; i++) {
    pthread_create(ticketAgent_tid + i, NULL, ticketAgent, NULL);
  }
  for (int i = 0; i < 2; i++) {
    pthread_join(ticketAgent_tid[i], NULL);
  }
  sleep(1);
  printf("The left ticket is %d\n", ticketAmout);
  sem_destroy(&mutex);
  return 0;
}
